package com.longteng.diamond.service;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.google.common.collect.Maps;
import com.longteng.diamond.dao.InterfaceTestMapper;
import com.longteng.diamond.dao.ProjectMapper;
import com.longteng.diamond.dao.TestCaseMapper;
import com.longteng.diamond.domain.*;
import com.longteng.diamond.po.RequestResult;
import com.longteng.diamond.po.TablePage;
import com.longteng.diamond.util.SessionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator
 * DATE : 2018/7/4
 * TIME : 23:39
 * PROJECT : java-lesson
 * PACKAGE : com.longteng.lesson
 *
 * @author <a href="mailto:taki@qq.com">Administrator</a>
 */
@Component
@Slf4j
public class ProjectService {
    @Autowired
    ProjectMapper projectMapper;
    @Autowired
    InterfaceTestMapper interfaceTestMapper;
    @Autowired
    TestCaseMapper testCaseMapper;
    @Autowired
    UserService userService;

    /**
     * 获取用户项目列表
     *
     * @param userId
     * @return void
     */
    public List<Project> getProjectByUserId(Integer userId) {
        return projectMapper.getProjectByUserId(userId);
    }

    /**
     * 获取用户项目列表
     *
     * @param project
     * @return TablePage
     */
    public TablePage getProject(Project project) {
        Map param = Maps.newHashMap();
        BeanUtils.copyProperties(param, project);
        Integer userId = SessionUtil.getSessionUser().getUserId();
        param.put("userId", userId);
        TablePage tablePage = new TablePage();
        try {
            List<Project> projectList = projectMapper.getProject(param);
            tablePage.setRows(projectList);
            long total = projectMapper.queryAllRow(param);
            tablePage.setTotal(total);
        } catch (Exception e) {
            log.error("查询接口列表异常", e);
        }
        return tablePage;
    }

    /**
     * 新增项目
     *
     * @param project
     * @return void
     */
    @Transactional(rollbackFor = Exception.class)
    public void addProject(Project project) throws Exception {
        User user = SessionUtil.getSessionUser();
        Integer userId = user.getUserId();
        Integer projectId = null;
        try {
            projectMapper.addProject(project);
            projectId = project.getProjectId();
        } catch (Exception e) {
            log.error("创建项目异常", e);
            throw new Exception("创建项目异常,请联系管理员");
        }
        try {
            /**创建项目者会把自己加入到项目中**/
            ProjectMember projectMember = new ProjectMember();
            projectMember.setProjectId(projectId);
            projectMember.setUserId(userId);
            projectMapper.addMember(projectMember);
        } catch (Exception e) {
            log.error("绑定项目成员异常!", e);
            throw new Exception("绑定项目成员异常,请联系管理员");
        }
//        userService.setUserProjectList(user, project);
    }

    /**
     * 校验项目是否存在接口、测试用例数据
     * @param projectId
     * @return RequestResult
     */
    public RequestResult deleteProjectCheck(Integer projectId) {
        RequestResult requestResult=new RequestResult();
        requestResult.setSuccess(true);
        InterfaceTest interfaceTest = new InterfaceTest();
        interfaceTest.setProjectId(projectId);
        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append("该项目已经存在");
        List<InterfaceTest> interfaceTestList = interfaceTestMapper.getInterfaceTest(interfaceTest);
        if(!CollectionUtils.isEmpty(interfaceTestList)){
            requestResult.setSuccess(false);
            stringBuffer.append("【接口】");
        }
        TestCase testCase=new TestCase();
        testCase.setProjectId(projectId);
        if(!CollectionUtils.isEmpty(testCaseMapper.getTestCase(testCase))){
            requestResult.setSuccess(false);
            stringBuffer.append("【测试用例】");
        }
        stringBuffer.append(",删除会导致数据丢失,确定要删除该项目吗?");
        requestResult.setMsg(stringBuffer.toString());
        return requestResult;
    }
    /**
     * 删除项目
     * @param projectId
     * @return
     */
    public void deleteProjectById(Integer projectId) {
        projectMapper.deleteProjectById(projectId);
        //把定时任务取消掉
    }
}
